package zzq;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import zzq.upload.UploadFtpConfigImpl;
import zzq.upload.UploadFtpLogAction;
import zzq.util.FtpAction;
import zzq.util.FtpConfig;
import zzq.util.Properties;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.stream.Stream;

// http://47.107.73.90:8888/python3.6.zip
public class DownloadClient {

    private FTPClient ftpClient;
    private Properties properties;
    private FtpAction ftpAction;
    private FtpConfig ftpConfig;
    private Set<String> fileSet;
    private Queue<String> fileQuery;
    public DownloadClient(FtpAction ftpAction, FtpConfig ftpConfig) {
        this.ftpAction = ftpAction;
        this.ftpConfig = ftpConfig;
        fileSet = new HashSet<>();
        fileQuery = new LinkedBlockingDeque<>();
    }

    public static void main(String[] args) {
        // 加载配置文件
        DownloadClient downloadClient = new DownloadClient(new UploadFtpLogAction(), new UploadFtpConfigImpl());
        downloadClient.init();
    }

    private void init() {
        this.properties = ftpConfig.loadProperties();
        System.out.println(this.properties.toString());
        final File file = new File(this.properties.getFolder());
        if (!file.exists()) {
            System.out.println("文件路径不存在");
            System.exit(-2);
        }
        if (!file.isDirectory()) {
            System.out.println("文件配置必须是路径");
            System.exit(-2);
        }
        Timer timer = new Timer();
        Stream.of(file.listFiles(ff -> !ff.isDirectory())).map(file1 -> file1.getName()).forEach(this.fileSet::add);
        System.out.println(this.fileSet);
//        for (int i = 0; i < files.length; i++) {
//            if (file.isDirectory()) {
//                continue;
//            }
//            fileSet.add(files[i].getName());
//        }
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Stream.of(Objects.requireNonNull(file.listFiles((f) -> !f.isDirectory() && !fileSet.contains(f.getName()))))
                .map(File::getName)
                .forEach(f ->{
                    System.out.println("detect file: " + f);
                    fileQuery.add(f);
                    fileSet.add(f);
                });
            }
        }, 1000, this.properties.getDetectCycle()*1000);
        System.out.println("开始监听文件准备上传....");
        while (true) {
            if (this.fileQuery.isEmpty()) {
                System.out.println("waiting.........");
                try {
                    Thread.sleep(1000 * this.properties.getDetectCycle());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                continue;
            }
            String file1 = this.fileQuery.poll();
            System.out.println("准备上传文件：" + file1);
            if (this.ftpClient == null || !this.ftpClient.isAvailable()) {
                this.ftpClient = ftpAction.login(properties);
            }

            try {
                ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println();
            this.ftpAction.uploadFile(this.properties.getFolder()+ "\\" + file1, "", this.ftpClient);
        }

    }


}
